package com.ecity.table {
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	import com.ecity.events.GunEvent;
	import com.ecity.util.UIFactory;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import gs.TweenLite;
	import gs.easing.*;

	
	public class PromptField extends TableDisplayObject {
		private static const CLASS_KEY:String = "prompt";
		private var cfgXml:XML = UIFactory.getConfiger();
		private var objIndex:int = 0;
		private var timer:Timer;
		
		private var fx:Number;
		private var fy:Number;
		
		public function PromptField(){
			
		}
		
		public function prompt(prompts:Array,stay:uint,interval:uint,isReplace:Boolean,isStay:Boolean):void{
			var p:DisplayObject = this.getObject(prompts[objIndex]);
			this.addChild(p);
			var itvl:Number = interval / 1000;
			if (objIndex == 0) itvl = 0;
			TweenLite.to(p, 1, { x:fx, y:fy, alpha:1, scaleX:1, scaleY:1, delay:itvl, onComplete:onFinishShow, onCompleteParams:[p, prompts, stay, interval, isReplace, isStay] } );
		}
		
		private function onFinishShow(p:Object,prompts:Array,stay:uint,interval:uint,isReplace:Boolean,isStay:Boolean):void {
			var scale:Number = 0;
			if (objIndex == prompts.length - 1) {
				if (isStay) {
					scale = 1;
				}
				TweenLite.to(p, 0.5, { scaleX:scale, scaleY:scale, delay:stay/1000,onComplete:onPormptComplete} );
				objIndex = 0;
			}else {
				if (!isReplace) {
					scale = 1;
				}
				objIndex++;
				TweenLite.to(p, 0.5, { scaleX:scale, scaleY:scale, delay:stay/1000,onComplete:prompt,onCompleteParams:[prompts, stay, interval, isReplace, isStay] } );
			}
		}
		
		private function onPormptComplete():void {
			TweenLite.to(this, 10, {scaleX:1, scaleY:1,onComplete:onClearPrompt});
		}
		
		private function onClearPrompt():void {
			this.dispatchEvent(new GunEvent(GunEvent.PUNISH_COMPLETE));
			while (this.numChildren) {
				this.removeChildAt(0);
			}
		}

		
		private function getObject(key:String):DisplayObject {
			var p:DisplayObject = UIFactory.getBitmapbyName(key) as DisplayObject;
				fx= cfgXml.layout[CLASS_KEY].pop.(@id==key).@x;
				fy = cfgXml.layout[CLASS_KEY].pop.(@id == key).@y;
				p.x = 0;
				p.y= 0;
				p.scaleX = 10;
				p.scaleY = 10;
				p.alpha = 0;
				p.rotation = cfgXml.layout[CLASS_KEY].pop.(@id==key).@rotation;
				p.visible = cfgXml.layout[CLASS_KEY].pop.(@id==key).@visible;
			return p;
		}
		


		
	}

}